import threading
from urllib.request import Request
from urllib.request import urlopen
from bs4 import BeautifulSoup


def catch_ip():
    url = 'http://www.xicidaili.com/nt/{}'

    head = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
    try:
        file = open('../file/ip.txt', 'w')
        for num in range(1, 31):
            get_url = url.format(num)
            print('search url is ' + get_url)
            request = Request(headers=head, url=get_url)
            respon = urlopen(request).read().decode('utf-8')
            soup = BeautifulSoup(respon, 'lxml')
            trs = soup.findAll('tr')
            for i in range(1,len(trs)):
                ip=trs[i]
                td=ip.findAll('td')
                str_ip=td[1].text+'\t'+td[2].text+'\n'
                file.write(str_ip)
                print(str_ip)
    except Exception as e:
        print(e)


if __name__ == '__main__':
    catch_ip()